﻿using System;
using ACompany.OrderProcessing.Model;
using ACompany.OrderProcessing.Presentation;

partial class OrderDetail : System.Web.UI.Page, IOrderDetailView
{

	private OrderPresenter _Presenter;

    protected void Page_Init(object sender, System.EventArgs e)
	{
		_Presenter = new OrderPresenter(this);
	}

    protected void Page_Load(object sender, System.EventArgs e)
	{
		if (!IsPostBack) {
			_Presenter.Display();
		}
	}

	public void ErrorMessage(string Message)
	{
		if (string.IsNullOrEmpty(Message)) {
			pnlError.Visible = false;
		} else {
			pnlError.Visible = true;
		}
        this.litErrorMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
	}

	public void InfoMessage(string Message)
	{
		if (string.IsNullOrEmpty(Message)) {
			pnlInfo.Visible = false;
		} else {
			pnlInfo.Visible = true;
		}
		this.litInfoMessage.Text = Message.Replace(Environment.NewLine, "<br/>");
	}


	public void ViewTitle(string title)
	{
	}

	public bool CancelOrderButtonVisible {
		set { this.btnCancel.Enabled = value; }
	}

	public ACompany.OrderProcessing.Model.Order Order {

		set {
			grdOrderItems.DataSource = value.Items;
			grdOrderItems.DataBind();

			this.litDespatchName.Text = value.Customer.Name;
			this.litDespatchStreet.Text = value.DespatchAddress.Street;
			this.litDespatchCity.Text = value.DespatchAddress.City;
			this.litDespatchCounty.Text = value.DespatchAddress.Country;
			this.litDespatchPostcode.Text = value.DespatchAddress.PostCode;

			this.litOrderId.Text = value.Id.ToString();
			this.litOrderDate.Text = value.OrderDate.ToLongDateString();
			litOrderTime.Text = value.OrderDate.ToShortTimeString();

			Session["OrderDetail"] = value;

		}
	}

	public Guid OrderId {
		get {
			Guid SelectedOrderId;
			if (string.IsNullOrEmpty(Request.QueryString["OrderId"])) {
				SelectedOrderId = Guid.Empty;
			} else {
				SelectedOrderId = new Guid(Request.QueryString["OrderId"]);
			}

			return SelectedOrderId;
		}
	}

	public bool ShipOrderButtonVisible {
		set { this.btnShip.Enabled = value; }
	}

    protected void btnShip_Click(object sender, System.EventArgs e)
	{
		_Presenter.Process((Order) Session["OrderDetail"]);
	}

    protected void btnCancel_Click(object sender, System.EventArgs e)
	{
		_Presenter.Cancel((Order)Session["OrderDetail"]);
	}
 
}